// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import LogModule from "../module/LogModule";

const {ccclass, property} = cc._decorator;

var ZORDER = cc.Enum({
    LoadingOrder: 1000,
    AlertOrder: 1001,
    ToastOrder: 1002,
    ViewsOrder: 100,
});

var ViewPath = {
    RankTrendView: 'RankTrendView',
    SWPlateView: 'SWPlateView',
    LookNodeView: 'LookNodeView',
    CalendarView: 'CalendarView',
    SelectPlateView: 'SelectPlateView',
    KLineNodeView: 'KLineNodeView',
    SearchStockView: 'SearchStockView',
    StockInfoView: 'StockInfoView',
    PlateAmountView: 'PlateAmountView',
    LookStocksView: 'LookStocksView',
    PlateCompareView: 'PlateCompareView',
    StockKLineView: 'StockKLineView',
    AnalyseStocksView: 'AnalyseStocksView',
    UpLimitLookView: 'UpLimitLookView',
    SearchConfigView: 'SearchConfigView',
    StockDaysRankView: 'StockDaysRankView',
    EmoteTrendView: 'EmoteTrendView',
    ThsDaysRankView: 'ThsDaysRankView',
};

@ccclass
export default class BaseView extends cc.Component {

    static Views = ViewPath;

    @property(cc.Label)
    labelTips: cc.Label = null;

    _pushFlag: boolean = false;

    show (params: Object = null) {
        this.node.active = true;
        if (this.labelTips) {
            this._pushFlag = true;
            LogModule.pushBindLabel(this.labelTips);
        }
    }

    hide () {
        this.node.active = false;
        if (this.labelTips && this._pushFlag) {
            LogModule.popBindLabel();
            this._pushFlag = false;
        }
    }

    onInit (params: Object) {

    }

    showToast (message: string, posType: number = 0) {
        let context = cc.director.getScene();
        if (cc.isValid(context) === false) {
            return;
        }
    
        if (cc.isValid(cc.find("ToastView",context))) {
            cc.find("ToastView",context).getComponent("ToastView").showToast({ message: message ,posType:posType});
            return;
        }
    
        cc.resources.load("prefabs/ToastView", function (err, toastPrefab: cc.Prefab) {
            if (err) {
                LogModule.log(err.message || err + '');
                return;
            }
            if (toastPrefab) {
                var newNode: cc.Node = cc.instantiate(toastPrefab);
                newNode.getComponent("ToastView").init({ message: message ,posType:posType});
                context.addChild(newNode, ZORDER.ToastOrder);
                return;
            }
        });
    }

    async createDialog (prefabPath: string, params: Object = null, isShow: boolean = true) {
        let context = cc.director.getScene();
        var prefabName = prefabPath.slice(prefabPath.lastIndexOf("/") + 1);
        let node = cc.find(prefabName, context);
        if (cc.isValid(node)) {
            node.active = true;
            node.zIndex = ZORDER.ViewsOrder;
            ZORDER.ViewsOrder++
            let baseView = node.getComponent(BaseView);
            if (isShow) {
                baseView.show(params);
            }
            return baseView;
        }

        var view: BaseView = await new Promise((resolve) => {
            cc.resources.load("prefabs/" + prefabPath, (err, prefab: cc.Prefab) => {
                if (err) {
                    LogModule.log(err.message || err + '');
                    resolve(null);
                    return;
                }
                if (cc.isValid(context)) {
                    var newNode = cc.instantiate(prefab);
                    newNode.zIndex = ZORDER.ViewsOrder;
                    ZORDER.ViewsOrder++
                    context.addChild(newNode);
                    let baseView = newNode.getComponent(BaseView);
                    if(params){
                        baseView.onInit(params);
                    }
                    if (isShow) {
                        newNode.getComponent(BaseView).show(params);
                    } else {
                        newNode.active = false;
                    }
                   
                    resolve(baseView);
                } else {
                    resolve(null);
                }
            });
        });
        return view;
    }
}
